﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using WebCache;

namespace WorkWriter
{
    public class AppContext
    {
        public static void Init()
        {
            CacheProviders.GlobalCache.Add(Keys.DICT_ADDRESS, LoadAllAddress(), LoadAllAddress);
            CacheProviders.GlobalCache.Add(Keys.DICT_SHORTCODE, LoadShortCode(), LoadShortCode);
        }

        private static List<Tuple<string, string, string>> LoadAllAddress()
        {
            CacheProviders.GlobalCache.Remove(Keys.DICT_ADDRESS);
            List<Tuple<string, string, string>> locations = new List<Tuple<string, string, string>>();
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath(@"~\bin\location.xml"));
            var root = doc.DocumentElement;
            foreach (XmlNode location in root.ChildNodes)
            {
                var code = location.SelectSingleNode("c").InnerText;
                var name = location.SelectSingleNode("n").InnerText;
                var parent = location.SelectSingleNode("p").InnerText;
                locations.Add(new Tuple<string, string, string>(code, name, parent));
            }
            return locations;
        }
        private static List<Tuple<string, string, string>> LoadShortCode()
        {
            CacheProviders.GlobalCache.Remove(Keys.DICT_SHORTCODE);
            List<Tuple<string, string, string>> shortCodes = new List<Tuple<string, string, string>>();
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath(@"~\bin\shortcode.xml"));
            var root = doc.DocumentElement;
            foreach (XmlNode item in root.ChildNodes)
            {
                var word = item.Attributes["c"].Value;
                var py = item.Attributes["py"].Value;
                var wb = item.Attributes["wb"].Value;
                shortCodes.Add(new Tuple<string, string, string>(word, py, wb));
            }
            return shortCodes;
        }
    }
}